using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using HaiFengShop.Middleware;
using HaiFengShop.Tools;
using HaiFengShop.Utils;
using HaiFengShop.Entity;
using HaiFengShop.ORM;
using Microsoft.OpenApi.Models;

namespace HaiFengShop
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "API Demo", Version = "v1" });
            });
            services.AddControllers();
            services.AddMemoryCache();
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            //services.AddDbContext<shopdbContext>(options =>
            //        options.UseMySql(Configuration.GetValue<string>("MySqlConnection")));

            services.AddScoped(con =>
            {
                return new SugarDbContext(Configuration.GetValue<string>("MySqlConnection"));
            });
            services.AddSingleton<ConfigExtensions>();
            services.AddTransient<ResponseMessage>();
            // 服务依赖注入
            services.ServeRegistered();
            // JWT
            services.AddScoped(context =>
            {
                var httpContextAccessor = (IHttpContextAccessor)context.GetService(typeof(IHttpContextAccessor));
                return httpContextAccessor.HttpContext.Request.ReadJWTCookie();
            });
            //跨域
            services.AddCors(options =>
                    options.AddPolicy("Cors",
                        corsBuilder =>
                            corsBuilder
                                .SetIsOriginAllowed(url => true)
                                .AllowAnyMethod()
                                .AllowAnyHeader()
                                .AllowCredentials())
                    );
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
                app.UseDeveloperExceptionPage();
            else
                app.UseExceptionHandler(GlobalException.ExceptionHandler);

            app.UseLogger();

            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            //app.UseAuthentication();

            //app.UseHttpsRedirection();

            app.UseRouting();

            //app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
